jQuery(document).ready(function(){
	
	$j("#acc_expand_all").bind("click", function(){
		
		$j(".accordianlist ul").slideDown("Fast");
		
	});
	
	$j("#acc_collapse_all").bind("click", function(){
		
		$j(".accordianlist ul").slideUp("Fast");
		
	});
	
	$j(".accordianlist").each(function(i){
		
		var id = $j(this).attr("id");
		
		$j("#" + id + " .title").bind("click", function(){
					
			$j("#" + id + " ul").slideToggle("Fast");
			
		});
		
	});
	
	$j("#pagepreview_box").draggable();
	
	function getSite(url)
	{
		$j.ajax({
			type: "POST",
			url:  "http://localhost/engine/backend/pages/createthumb",
			data: "data="+url,
			dataType: "json",
			success: function ( data, status ){
				$j("#sitelook").attr("src", data.url );
			}
		});
	}
	
	$j("#preview_close").bind("click", function(){
		
		$j("#pagepreview_box").fadeOut("normal");
	});
	
	
	$j(".accordianlist ul li").each(function(i){
		var elm = $j(this).attr("id");
		
		$j("#"+elm).bind("click", function(){
					
			$j("#"+elm+" div").fadeIn("normal");
			
		});
		
		$j("#"+elm+" .delete_btn").bind("click", function(){
			
			var conf = confirm ( "Are you sure? If page is deleted, there is no backup or somthing like that!" );
			
			if ( conf )
			{
				$j("#progress").fadeIn("normal");
				var arrdata = $j("#"+elm+" .delete_btn").attr("title");
				$j("#pmessage").text( "Please wait untill we delete current page..." );
				$j.ajax({
					type: "POST",
					url:  "http://localhost/engine/backend/pages/delete",
					data: "data="+arrdata,
					dataType: "json",
					error: function (XMLHttpRequest, textStatus, errorThrown) {
						$j("#progress").addClass("progress-error");
						$j("#pmessage").text( "We can't open page. There is some problem with the script!" );
						setTimeout ( '$j("#progress").fadeOut("normal");', 5000 );
					},
					success: function ( data, status ){
						$j("#pmessage").text( data.message );
						setTimeout ( '$j("#progress").fadeOut("normal");', 3000 );
						$j("#"+elm).effect("drop", { direction: "up" });
					}
				});
			}

		});
		
		$j("#"+elm+" .preview_btn").bind("click", function(){
			
			var linkloc = $j("#"+elm+" .preview_btn").attr("title");
			
			$j("#pagepreview_box").fadeIn("normal");
			
			getSite(linkloc);
		});
		
		$j("#"+elm).bind("click", function(){

			$j("#description_box").slideToggle("Fast");
			
		});
		
		$j("#"+elm).bind("mouseleave", function(){
					
			$j("#"+elm+" div").fadeOut("normal");
			
						
			$j("#description_box").fadeOut("normal");
		});
	});
	
	


});